#!/bin/sh
#bash <(curl -L https://gitee.com/christianswift/surveillance-station-9/raw/main/9.2.0_11289/license/remove_license)


# 定义一个函数来处理恢复文件操作
process_file() {
  local dir="$1"
  local file="$2"
  local backup="$3"
  local mode="$4"

  cd "$dir" || exit

  # 检查备份文件是否存在
  if [ -e "./$backup" ]; then
    rm "./$file"
    mv "./$backup" "./$file"
    chown SurveillanceStation:SurveillanceStation "./$file"
    chmod "$mode" "./$file"
  else
    echo "Error: Backup file $backup does not exist in $dir"
  fi
}
# 解除屏蔽认证服务器
unblock_license_server() {
  if grep -q "synosurveillance.synology.com" /etc/hosts; then
    echo "Unblocking license server: synosurveillance.synology.com"
    sudo sed -i '/synosurveillance.synology.com/d' /etc/hosts
  else
    echo "License server not blocked: synosurveillance.synology.com."
  fi
}

/usr/syno/bin/synopkg stop SurveillanceStation
sleep 5


# 调用函数处理每个文件
process_file /var/packages/SurveillanceStation/target/lib   libssutils.so     libssutils.so_backup    0644
process_file /var/packages/SurveillanceStation/target/sbin  sscmshostd        sscmshostd_backup       0755
process_file /var/packages/SurveillanceStation/target/sbin  sscored           sscored_backup          0755
process_file /var/packages/SurveillanceStation/target/sbin  ssdaemonmonitord  ssdaemonmonitord_backup 0755
process_file /var/packages/SurveillanceStation/target/sbin  ssexechelperd     ssexechelperd_backup    0755
process_file /var/packages/SurveillanceStation/target/sbin  ssroutined        ssroutined_backup       0755
process_file /var/packages/SurveillanceStation/target/sbin  ssmessaged        ssmessaged_backup       0755
unblock_license_server
 

sleep 5
/usr/syno/bin/synopkg start SurveillanceStation
